// 03personCenter/personCenter.js
var co = require('../../js/common.js')
var app = getApp()
var _this
Page({
   data: {
      userInfo: '',
   },

   onLoad: function (opt) {
      _this = this
      console.log('【app.aData.userInfo】', app.aData.userInfo)
      this.setData({ userInfo: app.aData.userInfo })

      app.setNoticeUpdateUserInfo(function (res) {
         console.log('【个人中心收到通知】res', res)
         _this.setData({ userInfo: res })
      })
   },

   reGetMyInfo: function () {
      if (co.empty(this.data.userInfo.user_id)) {
         this.toReg()
         return
      }
      var param = { user_id: app.aData.userInfo.user_id }
      co.getShareApi('user/mg/getUserInfo2', param, function (res) {
         res.data.openid = app.aData.userInfo.openid  //因为数据库存的openid不确定是哪个appid的，固不能用
         _this.setData({ userInfo: res.data })
         app.aData.userInfo = res.data
         wx.setStorageSync('userInfo', res.data)
         co.toast('个人信息已刷新')
         console.log("【个人信息app.aData.userInfo】", app.aData.userInfo);
      })
   },
   //去注册的提示
   toReg: function () {
      wx.showModal({
         title: '温馨提示',
         content: '需要先允许授权注册，才能正常使用哦！',
         showCancel: true,
         cancelText: '取消',
         cancelColor: '',
         confirmText: '现在注册',
         confirmColor: '#f60',
         success: function (res) {
            if (res.confirm) {
               wx.navigateTo({ url: '/login/login' })
            }
         }
      })
   },

   getPhoneNumber: function (e) {
      console.log("【手机号加密数据】", e);
      //拒绝= 'getPhoneNumber:fail user deny'
      //取消= 'getPhoneNumber:fail user canceled'
      //安卓取消= 'getPhoneNumber:fail:cancel to verify sms'
      if (e.detail.errMsg == 'getPhoneNumber:ok') {
         var param = {
            appid: app.aData.app.appid,
            session_key: wx.getStorageSync('session_key'),
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
         }
         co.getShareApi('user/mg/decrypt', param, function (res) {
            console.log('【解密结果】', res);
            if (res.data == 41003) {
               app.getUserInfo(function (result) {
                  if (result == 'reget_session_key') {
                     _this.getPhoneNumber(e)
                  }
               }, 'getPhoneNumber')
            } else if (res.code == 0) {
               _this.updateUserPhone(res.data.purePhoneNumber)
            }
         })
      }
   },

   updateUserPhone: function (phone) {
      var param = {
         unionid: app.aData.userInfo.unionid,
         phone: phone,
         phone_auth: 1,
      }
      co.getShareApi('user/mg/updateUserInfo', param, function (res) {
         console.log('【更新结果】', res);
         app.aData.userInfo = res.data
         _this.setData({ userInfo: res.data })
      })
   },

   toInviter: function () {
      if (co.empty(this.data.userInfo.user_id)) {
         this.toReg()
         return
      }
      wx.navigateTo({ url: '/pc/02user/inviter' })
   },

   toMyPostedOrder: function () {
      if (co.empty(this.data.userInfo.user_id)) {
         this.toReg()
         return
      }
      wx.navigateTo({ url: '/pc/04list/myPostedOrder' })
   },

   toMyGotOrder: function (e) {
      if (co.empty(this.data.userInfo.user_id)) {
         this.toReg()
         return
      }
      wx.navigateTo({ url: '/pc/05my/gotOrder?id=' + e.currentTarget.id })
   },

   toMyFinance: function (e) {
      if (co.empty(this.data.userInfo.user_id)) {
         this.toReg()
         return
      }
      wx.navigateTo({ url: '/pc/06my/finance' })
   },
   //去我的余额
   toMyBalance: function () {
      if (co.empty(this.data.userInfo.user_id)) {
         this.toReg()
         return
      }
      wx.navigateTo({ url: '/pc/07my/balance' })
   },
   //去认证为师傅
   toApplyMaster: function () {
      if (co.empty(this.data.userInfo.user_id)) {
         this.toReg()
         return
      }
      var toUrl = '/pc/03apply/beMasterWorker?unionid='
      wx.navigateTo({ url: toUrl + app.aData.userInfo.unionid })
   },
   //去考试
   toExam: function () {
      if (co.empty(this.data.userInfo.user_id)) {
         this.toReg()
         return
      }
      wx.navigateTo({ url: '/master/exam/question' })
   },
   //平台业务流程
   toFlowChart: function () {
      wx.navigateTo({ url: '/platform/01business/flowChart' })
   },
   //关于我们
   toAboutUs: function () {
      wx.navigateTo({ url: '/platform/02/aboutUs' })
   },
   //去邀请的好处
   toInvitationBenefits: function () {
      wx.navigateTo({
         url: '/platform/03/invitationBenefits',
      })
   }
})